from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
import urllib
import urllib2
import locale
import codecs
import time
import sys
import re
import selenium.webdriver.chrome.service as service
#import os


locale.setlocale( locale.LC_ALL, 'en_US.UTF-8' ) 
# chromedriver = "/home/ruian/Documents/Webcrawler/chromium/oldchromedriver/chromedriver"
chromedriver = "/home/intern/chromium/chromedriver"
service = service.Service(chromedriver)
service.start()
# capabilities = {'chrome.binary': '/home/ruian/Documents/Webcrawler/chromium/home/src_tarball/tarball/chromium/src/out/Release/chrome'}
capabilities = webdriver.DesiredCapabilities.CHROME
print capabilities
capabilities['executablePath'] = '/home/intern/chromium/src/out/Release/chrome'
print capabilities
# capabilities = {'chrome.binary': '/home/intern/chromium/src/out/Release/chrome'}
browser = webdriver.Remote(service.service_url, capabilities)
#print driver.get('http://www.google.com')


#browser = webdriver.Chrome(chromedriver)


#browser = webdriver.Chrome()
#browser.delete_all_cookies()

globaltop = 'alexaglobaltop.txt'
ustop = 'alexaustop.txt'

globaltop = codecs.open(globaltop, encoding = 'utf-8', mode = 'w')
ustop = codecs.open(ustop, encoding = 'utf-8', mode = 'w')

urlp1 = "http://www.alexa.com/"
urlp2 = "topsites/global;"
pages = []
#browser.get('http://www.alexa.com/topsites/global;0')
browser.get('http://www.baidu.com')
service.stop()
service.start()
for i in range(0, 4):
	pages.append(str(i))
for urlp3 in pages:
	url = urlp1 + urlp2 + urlp3
	browser.get(url)
"""
	site_listing = browser.find_elements_by_class_name("site-listing")
	for website in site_listing:
		name = website.find_element_by_tag_name("h2").find_element_by_tag_name("a").text
		link = website.find_element_by_class_name("topsites-label").text
		globaltop.write(name + '\n' + link + '\n\n')
"""
"""
urlp2 = "topsites/countries;"
pages = []
for i in range(0, 4):
	pages.append(str(i))
urlp4 = "/US"
for urlp3 in pages:
	url = urlp1 + urlp2 + urlp3 + urlp4
	browser.get(url)
	site_listing = browser.find_elements_by_class_name("site-listing")
	for website in site_listing:
		name = website.find_element_by_tag_name("h2").find_element_by_tag_name("a").text
		link = website.find_element_by_class_name("topsites-label").text
		ustop.write(name + '\n' + link + '\n\n')
browser.close()
"""

